package com.xiguaji.tiktok.v1.web.tiktok.controller;

import com.xiguaji.boot.toolkit.HttpUtil;
import com.xiguaji.boot.toolkit.JsonUtil;
import com.xiguaji.tiktok.utils.CheckMobile;
import com.xiguaji.tiktok.utils.jwt.JwtUtil;
import com.xiguaji.tiktok.v1.product.dto.ProductResponse;
import com.xiguaji.tiktok.v1.product.service.ProductService;
import com.xiguaji.tiktok.v1.user.dto.UserReturnDto;
import com.xiguaji.tiktok.v1.user.model.User;
import com.xiguaji.tiktok.v1.user.service.UserService;
import com.xiguaji.tiktok.v1.web.Tab;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@Slf4j
public class ChargeController {

    @Value("${jwt.head}")
    String authorized;

    @Value("${server.domain}")
    String serverDomain;

    @Autowired
    JwtUtil jwtUtil;

    @Autowired
    ProductService productService;

    @Autowired
    UserService userService;

    /**
     * 产品页面
     * @return
     */
    @GetMapping("/pricing")
    public ModelAndView pricing(){
        ModelAndView modelAndView = new ModelAndView("pricing/index");
        modelAndView.addObject("isMobile", CheckMobile.check());
        modelAndView.addObject("tab", Tab.PRICING.getValue());
        modelAndView.addObject("contextPath",serverDomain);
        UserReturnDto user = getUser();
        modelAndView.addObject("login", user != null);
        modelAndView.addObject("user", user);
        List<ProductResponse> report = productService.getReportProductList();
        modelAndView.addObject("reportProduct",report);
        List<ProductResponse> productResponses;

        if(null == user){
            productResponses = productService.getVersionProductList(0);
        }else {
            productResponses = productService.getVersionProductList(getUser().getId());
        }
        productResponses.set(0,report.get(0));
        modelAndView.addObject("versionProduct",productResponses);
        return modelAndView;
    }


    @RequestMapping(value = "pricing/{productId}",method = RequestMethod.GET)
    public ModelAndView getProductById(@PathVariable long productId){

        ModelAndView modelAndView = new ModelAndView("pricing/order/index");
        modelAndView.addObject("isMobile", CheckMobile.check());
        modelAndView.addObject("tab", Tab.PRICING.getValue());
        modelAndView.addObject("contextPath",serverDomain);
        UserReturnDto user = getUser();
        modelAndView.addObject("login", user != null);
        modelAndView.addObject("user", user);
        modelAndView.addObject("reportProduct",productService.getReportProductList());
        String curName = "SINGLE";
        List<ProductResponse> productResponses;

        if(null == user){
            productResponses = productService.getVersionProductList(0);
        }else {
            productResponses = productService.getVersionProductList(getUser().getId());
        }

        modelAndView.addObject("curProduct",productId);
        ProductResponse curVersionProduct = null;

        for (ProductResponse productResponse : productResponses) {
            if(productResponse.getId() == productId){
                curVersionProduct = productResponse;
                curName = curVersionProduct.getName();
            }
        }


        modelAndView.addObject("versionProduct",curVersionProduct);
        modelAndView.addObject("curName",curName);
        return modelAndView;


        /*if(productId <= 4 ){
            List<ProductResponse> versionProductList = productService.getVersionProductList(getUser().getId());
            for (ProductResponse productResponse : versionProductList) {
                if(productId == productResponse.getId()){
                    return Ret.success("OK",productResponse);
                }
            }
        }else if(productId == 5){
            return Ret.success("OK",productService.getReportProductList());
        }*/

    }

    private UserReturnDto getUser() {
        String token = HttpUtil.getCookie(authorized);

        if (StringUtils.isEmpty(token)) {
            return null;
        }
        User user = JsonUtil.jsonParse(User.class, JsonUtil.parseJson(jwtUtil.getJwtUser(token).getUser()));
        return userService.getUser(user.getId());
    }
}
